require 'View/UIBase/UIBase'

UIPrivilegeConfirm = UIBase.New(PanelResNames.UIPrivilegeConfirm)

function UIPrivilegeConfirm.Init(func, id, subId)
    UIPrivilegeConfirm.activeId = id;
    UIPrivilegeConfirm.subId = subId;
    UIBase.Init(UIPrivilegeConfirm, func);
end

function UIPrivilegeConfirm:OnCreate()
    self.Btn_Confirm = self:GetChild("Btn_Confirm")
    self.Btn_Confirm.title = getLanguage("OptionalPool_Btn_Buy")
    self.Btn_Cancel = self:GetChild("Btn_Cancel")
    self.Btn_Cancel.title = getLanguage("Btn_Cancel")
    self.buyNum = self:GetChild("num")
    self.list = self:GetChild("list")
    self.stateController = self:GetController("state")

    self:RegisterOnClick(self.Btn_Cancel,self.OnClose);
    self:RegisterOnClick(self:GetChild("Btn_BGClose"),self.OnClose);
    self:RegisterOnClick(self.Btn_Confirm,self.OnBuy);
end

-- 显示
function UIPrivilegeConfirm:OnEnable()
    if self.activeId == nil or self.subId == nil then
        self:OnBack();
    else
        self:Refresh();
    end
end

function UIPrivilegeConfirm:Refresh()
    self.severData, self.itemData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    self.cheekItemId = self.subId;
    self.nowItem = {};
    for k, v in pairs(self.itemData) do
        if v.f_ItemId == self.subId then
            self.nowItem = v;
            break;
        end
    end
    self.cheekItemProductId = self.nowItem.f_ProductId;
    self.cheekItemMoney = self.nowItem.f_Price;
    if self.severData.overTime <= HelperFunc.GetServerTime() then
        self.stateController.selectedIndex = 1;
        self.list:RemoveChildrenToPool();
        self.modelTableData.f_NobleBuff = decodeJsonStrToTabel(self.modelTableData.f_NobleBuff)
        for k, v in pairs(self.modelTableData.f_NobleBuff) do
            local item = self.list:AddItemFromPool()
            self:updateDescItem(item, v, 1);
        end
        self.modelTableData.f_NobleRightDesc = decodeJsonStrToTabel(self.modelTableData.f_NobleRightDesc);
        for k, v in pairs(self.modelTableData.f_NobleRightDesc) do
            local item = self.list:AddItemFromPool()
            self:updateDescItem(item, v, 2);
        end
    else
        self.stateController.selectedIndex = 0;
        self.buyNum.text = self.nowItem.f_Duration.."天";
    end
end

function UIPrivilegeConfirm:updateDescItem(itemObj, tableData, type)
    local text = itemObj:GetChild("Txt_Check");
    if type == 1 then
        local buff = G.dataTable["t_ecobuff"][tostring(tableData)];
        if buff ~= nil then
            text.text = getLanguage(buff.f_BuffDesc);
        else
            text.text = "";
        end
    else
        text.text = getLanguage(tableData);
    end
end

function UIPrivilegeConfirm:OnBuy()
    logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, self.cheekItemId, self.cheekItemProductId, 1, self.cheekItemMoney, self.onBuySuccess, self);
    self:OnClose();
end

function UIPrivilegeConfirm:onBuySuccess(id, index)
end

-- 关闭
function UIPrivilegeConfirm:OnClose()
    self:OnBack();
end

-- 隐藏
function UIPrivilegeConfirm:OnDisable()
end

-- 卸载
function UIPrivilegeConfirm:OnDestroy()
end

return UIPrivilegeConfirm